package com.rookie.opcua.controller;

import com.rookie.opcua.dto.NodeTreeDTO;
import com.rookie.opcua.dto.ResponseDTO;
import com.rookie.opcua.dto.ServerStatusDTO;
import com.rookie.opcua.dto.SimulationDTO;
import com.rookie.opcua.entity.DataSimulation;
import com.rookie.opcua.entity.DataSubscription;
import com.rookie.opcua.service.ReadDeviceService;
import com.rookie.opcua.support.OpcUaOperationSupport;
import com.rookie.opcua.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.eclipse.milo.opcua.sdk.client.api.nodes.Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yugo
 */
@RestController
@RequestMapping("/opc-ua")
@Api(value = "opcua 协议接口", tags = "opcua 信息模型查看接口")
@AllArgsConstructor
public class OpcUaController {

/*
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
*/

    @Autowired
    private OpcUaOperationSupport opcUaOperationSupport;

    private final ReadDeviceService readDeviceService;

    //获取服务器状态
    @ApiOperation(value = "获取服务状态", notes = "获取服务状态")
    @GetMapping("/status")
    public ServerStatusDTO getStatus() {
        try {
            return opcUaOperationSupport.getStatus();
        } catch (Exception e) {
            return null;
        }
    }

    @ApiOperation(value = "读取单个数据", notes = "读取单个数据")
    @GetMapping("/getNodeValue")
    @ApiImplicitParams({
            @ApiImplicitParam(name="nameSpaceIndex",value="节点空间index",required=true,paramType="Integer"),
            @ApiImplicitParam(name="name",value="节点名称",required=true,paramType="String")
    })
    public DataSimulation getStatus(
            Integer nameSpaceIndex,
            String name
    ) {
        try {
            return opcUaOperationSupport.getNodeValue(nameSpaceIndex,name);
        } catch (Exception e) {
            return null;
        }
    }


/*    @GetMapping("/write/node/value")
    @ApiOperation(value = "写入数据", notes = "写入数据")
    public String writeValue() {
        try {
            opcUaOperationSupport.writeNodeValue();
            return "success";
        } catch (Exception e) {
            return "fail";
        }
    }*/

    //列出节点  遍历获取
    @GetMapping("/browse/node")
    @ApiOperation(value = "获取所有节点", notes = "获取所有节点")
    public List<String> browseNode() {
        try {
            return opcUaOperationSupport.browseNode();
        } catch (Exception e) {
            return null;
        }
    }

    //订阅变量
    @GetMapping("/subscription")
    @ApiOperation(value = "订阅节点", notes = "订阅节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name="nameSpaceIndex",value="节点空间index",required=true,paramType="Integer"),
            @ApiImplicitParam(name="name",value="节点名称",required=true,paramType="String")
    })
    public String createSubscription(
            Integer nameSpaceIndex,
            String  name    //订阅名称
    ) {
        try {
            return  opcUaOperationSupport.createSubscription(nameSpaceIndex,name);
        } catch (Exception e) {
            return null;
        }
    }

    @ApiOperation(value = "读取订阅的最近50条数据", notes = "读取订阅的最近50条数据")
    @GetMapping("/getSubValue")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value="节点名称",required=true,paramType="String")
    })
    public List<DataSubscription> getSubValue(
            Integer nameSpaceIndex,
            String name    //数据名
    ) {
        try {
            return opcUaOperationSupport.getSubValue(nameSpaceIndex,name);
        } catch (Exception e) {
            return null;
        }
    }

    @ApiOperation(value = "查看单个节点历史数据", notes = "查看单个节点历史数据")
    @GetMapping("/history")
    @ApiImplicitParams({
            @ApiImplicitParam(name="nameSpaceIndex",value="节点空间index",required=true,paramType="Integer"),
            @ApiImplicitParam(name="name",value="节点名称",required=true,paramType="String")
    })
    public List historyRead(
            Integer nameSpaceIndex,    //空间index
            String nodeName    //节点名称
    ) {
        try {
            return opcUaOperationSupport.historyRead(nameSpaceIndex,nodeName);
        } catch (Exception e) {
            return null;
        }
    }

    //读取节点结构数据
    @GetMapping("/browse/oneSpace")
    @ApiOperation(value = "获取单个space下节点", notes = "获取单个space下节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value="节点空间名称",required=true,paramType="Integer")
    })
    public ResponseDTO getDevice(String name) {
        try {
            return ResponseDTO.ok(readDeviceService.getNodeStructure(name));
        } catch (Exception e) {
            return ResponseDTO.failed();
        }
    }
}
